<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 创建函数 `truncate(str, maxlength)` 来检查 `str` 的长度，
        // 如果超过 `maxlength` —— 应使用 `"…"` 来代替 `str` 的结尾部分，长度仍然等于 `maxlength`。
        function truncate(str, maxlength) {
            if (str.length > maxlength) {
                //Unicode编码
                str = str.slice(0, maxlength - 1) + '\u2026';
            }
            return str;
        }
        var str1 = truncate("What I'd like to tell on this topic is:", 20); // "What I'd like to te…"
        var str2 = truncate("Hi everyone!", 20); // "Hi everyone!"
        console.log(str1);
        console.log(str1.length);
        console.log(str2);
        console.log(str2.length);
    </script>
</body>

</html>